﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:00
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  CategoryRepository : Repository<Category, Category, TbCategory>, ICategoryRepository<Category>
    {
        public override Category CreateEntity()
        {
            return new Category { };
        }
        public override IQueryable<Category> Get()
        {
            return MapQueryable(Entities.TbCategory);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbCategory).Count();
        }
        public override IQueryable<Category> Get(Expression<Func<Category, bool>> expression)
        {
            return MapQueryable(Entities.TbCategory).Where(expression);
        }
        public override IQueryable<Category> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbCategory), sort, order);
        }
        public override IQueryable<Category> GetSort(Expression<Func<Category, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbCategory).Where(expression), sort, order);
        }
        public override void Add(Category t)
        {
            Entities.AddToTbCategory(ReverseMapModel(t));
        }
        public override void Update(Category t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Category t)
        {
            if (Entities.TbCategory.Any(o => o.CoCategoryID == t.Id))
            {

            }
            else
            {
                Entities.AddToTbCategory(ReverseMapModel(t));
            }
        }
        public override void Delete(Category t)
        {
            Entities.TbCategory.Where(o => o.CoCategoryID == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Category, bool>> expression)
        {
            MapQueryable(Entities.TbCategory).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Category> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Category>();
        }
      
        public override sealed IQueryable<Category> MapQueryable(IQueryable<TbCategory> queryable)
        {
            return queryable.Select(o => new Category 
            {
         
	           Id=o.CoCategoryID,
	           Name = o.CoName 
,
	           NameEn = o.CoNameEn 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Code = o.CoCode 
,
	           Sort = o.CoSort??0 
,
	           Type = o.CoType??0 
,
	           Depth = o.CoDepth??0 
,
	           ParentId = o.CoParentId 
,
	           MarkEn = o.CoMarkEn 
,
	           Mark = o.CoMark 
,
	           IsSystem = o.CoIsSystem??false 
,
	           Culture = o.CoCulture 
            
            });
        }
      
        public override sealed IQueryable<TbCategory> ReverseMapQueryable(IQueryable<Category> queryable)
        {
            return queryable.Select(o => new TbCategory 
            { 
        
	           CoCategoryID=o.Id,
	           CoName = o.Name
,
	           CoNameEn = o.NameEn
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCode = o.Code
,
	           CoSort = o.Sort
,
	           CoType = o.Type
,
	           CoDepth = o.Depth
,
	           CoParentId = o.ParentId
,
	           CoMarkEn = o.MarkEn
,
	           CoMark = o.Mark
,
	           CoIsSystem = o.IsSystem
,
	           CoCulture = o.Culture
  
            
            });
        }
      
        public override sealed Category MapModel(TbCategory model)
        {
            return new Category {
	           Id=model.CoCategoryID,
	           Name = model.CoName
,
	           NameEn = model.CoNameEn
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Code = model.CoCode
,
	           Sort = model.CoSort??0
,
	           Type = model.CoType??0
,
	           Depth = model.CoDepth??0
,
	           ParentId = model.CoParentId
,
	           MarkEn = model.CoMarkEn
,
	           Mark = model.CoMark
,
	           IsSystem = model.CoIsSystem??false
,
	           Culture = model.CoCulture
            };
        }
        
        public override sealed TbCategory ReverseMapModel(Category model)
        {
            return new TbCategory { 
	           CoCategoryID=model.Id,
	           CoName = model.Name
	        
,
	           CoNameEn = model.NameEn
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCode = model.Code
	        
,
	           CoSort = model.Sort
	        
,
	           CoType = model.Type
	        
,
	           CoDepth = model.Depth
	        
,
	           CoParentId = model.ParentId
	        
,
	           CoMarkEn = model.MarkEn
	        
,
	           CoMark = model.Mark
	        
,
	           CoIsSystem = model.IsSystem
	        
,
	           CoCulture = model.Culture
	        
            
            
            };
        }


    }
}



